<?php
namespace Home\Controller;
use Home\Controller\BaseController;
// +----------------------------------------------------------------------
// | Date:2017年1月5日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class MyFavoriteController extends BaseController{
    
    public function _initialize(){
        parent::_initialize();
        $this->assign('showSearchbar','hide');//隐藏顶部的搜索框
    }
    
    /**
     * 我的收藏夹
     * 
     * date:2017年1月5日
     * author: EK_熊
     */
    public function index(){
        
       $favModel = D('Favorite'); 
       $search_title = I('keyword');
       if ($search_title) {
           $where['pro.title|pro.summary'] = ['like',"%{$search_title}%"];
       }
       $field = 'pro.id,summary,title,image_id,fav_id,t_lx,t_jx';
       $where['fav.uid'] = $this->uid;
       $where['fav.status'] = 1;
       $labelID = I('labelid');
       $title = I('title');
       if(!empty($labelID) || $labelID === '0') $where['fav.label_id'] = $labelID; 
       if(!empty($title)) $where['pro.title'] = ['like',"%$title%"];
       
       $order = 'fav.update_time desc';
       $count = $favModel->alias('fav')->join('LEFT JOIN __PRODUCT__ pro ON pro.id = fav.pro_id')->where($where)->count();
       $page = new \Think\Page($count,20);
       $showPage = $page->show();
       $list =  $favModel->alias('fav')
                         ->join('LEFT JOIN __PRODUCT__ pro ON pro.id = fav.pro_id')
                         ->where($where)
                         ->group('fav.pro_id')
                         ->order($order)
                         ->limit($page->firstRow, $page->listRows)
                         ->field($field)->select();
       $list = D('Product')->getCover($list);//拼接封面图
       
       $list = $favModel->label_append_by_proid($list);//拼接标签
       
       //获取当前用户的全部标签
       $userLabel = $favModel->label_list_user_all();
       $this->assign('list',$list);
       $this->assign('userlabel',$userLabel);
       $this->assign('page',$showPage);
       $this->display(); 
    }
    
    /**
     * 移除收藏记录
     * 
     * date:2017年1月10日
     * author: EK_熊
     */
    public function remove(){
        $favId = I('favid');
        $ret = M('ProductFavorite')->where(['fav_id'=>$favId])->setField('status',0);
        if (!$ret) $this->error('网络异常，操作失败！');
        $this->success('操作完成！');
    }
    
    
    /**
     * 收藏夹：编辑修改标签
     * 
     * date:2017年1月10日
     * author: EK_熊
     */
    public function labelEdit(){
        $labelid = I('labelid');
        $labelname = I('labelname');
        if (empty($labelname)) $this->error('参数不能为空！');
        $ret = M('ProductLabel')->where(['label_id'=>$labelid])->setField('label_name',trim($labelname));
        if ($ret){
           $this->success('操作完成');
        }else{
            $this->error('网络异常，修改失败！');
        }
    }
    
    
    /**
     * 我的收藏夹：删除标签
     * 
     * date:2017年1月10日
     * author: EK_熊
     */
    public function labelDel(){
        $labelid = I('labelid');   //标签id
        $favnum = I('favnum');   //文章收藏数量
        $labelModel = M('ProductLabel');
        $favlModel = M('ProductFavorite');
        do{
            $errmsg = '';
            $labelModel->startTrans();
            $del_label = $labelModel->where(['label_id'=>$labelid,'uid'=>$this->uid])->delete();
            if (!$del_label) {
                $errmsg = '标签数据删除失败！';
                break;
            }
            if ($favnum > 0) {
                $del_fav = $favlModel->where(['label_id'=>$labelid,'uid'=>$this->uid])->setField('label_id',0);
                if (!$del_fav) {
                    $errmsg = '收藏数据修改失败！';
                    break;                
                }
            }
        }while(false);
        if ($errmsg){
            $labelModel->rollback();
            $this->error($errmsg); 
        }else{
            $labelModel->commit();
            $this->success('操作完成');
        }
        
    }
    
    
}